package com.example.newwork

import com.example.newwork.flow.FlowCallAdapterFactory
import com.example.newwork.livedata.LiveDataCallAdapterFactory
import okhttp3.OkHttp
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object NetService {
    fun initFlowService():Retrofit{
       val  okHttpClient = OkHttpClient()
            .newBuilder()
            .build()

       return Retrofit.Builder()
           .baseUrl(MainContains.BASE_URL)
           .client(okHttpClient)
           .addConverterFactory(GsonConverterFactory.create())
           .addCallAdapterFactory(FlowCallAdapterFactory())
           .build()

    }

    fun initLiveDataService():Retrofit{
        val  okHttpClient = OkHttpClient()
            .newBuilder()
            .build()

        return Retrofit.Builder()
            .baseUrl(MainContains.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(LiveDataCallAdapterFactory())
            .build()

    }

    fun initService():Retrofit{
        val  okHttpClient = OkHttpClient()
            .newBuilder()
            .build()

        return Retrofit.Builder()
            .baseUrl(MainContains.BASE_URL)
            .client(okHttpClient)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    }



}

inline fun < reified T> getFlowService(t : Class<T>): T = NetService.initFlowService().create(t)

inline fun < reified T> getLiveDataService(t : Class<T>): T = NetService.initLiveDataService().create(t)

inline fun < reified T> getService(t : Class<T>): T = NetService.initService().create(t)